#include "realmachine.h"

template<> RealMachine* Singleton<RealMachine>::msSingleton = 0;
RealMachine* RealMachine::getSingletonPtr(void)
{
    return msSingleton;
}
RealMachine& RealMachine::getSingleton(void)
{
    assert( msSingleton );  return ( *msSingleton );
}
RealMachine::RealMachine()
{
    memory = new WordVector();

    Word temp;
    temp[0] = 0x00;
    temp[1] = 0x00;
    temp[2] = 0x00;
    temp[3] = 0x00;

    for(int i = 0; i < MEMORY_BLOCKS * BLOCK_SIZE; i++)
    {
        (*memory).push_back(temp);
    }

    processor = new Processor();

    processor->MODE = 0x01;

    processor->memory = memory;
}
RealMachine::~RealMachine()
{
    delete processor;
    delete memory;
}

void RealMachine::Run() {
    if(processor->MODE == 0x1) {
        OS::getSingleton().Run();
    }else {
        processor->Run();
    }
}
//-----------------------------------------------------------------------

void RealMachine::LoadProcessor(ProcessorContainer* rContainer) {

    if(processor && rContainer) {
        processor->RA = rContainer->getRA();
        processor->RB = rContainer->getRB();
        processor->IO = rContainer->getIO();
        processor->NEXT = rContainer->getNEXT();
        processor->SR = rContainer->getSR();
        processor->SPR = rContainer->getSPR();
        processor->PL = rContainer->getPL();
        processor->BAR = rContainer->getBAR();
        processor->PS = rContainer->getPS();
        processor->MODE = rContainer->getMODE();
        processor->PPR = rContainer->getPPR();
        processor->TIME = rContainer->getTIME();
        processor->SF = rContainer->getSF();
    }else {
        LogManager::getSingleton().Error("RealMachine::LoadProcessor", "processor or processorcontainer ptr is null");
    }
}

void RealMachine::UnloadProcessor(ProcessorContainer* rContainer) {

    if(processor && rContainer) {
        rContainer->setRA(processor->RA);
        rContainer->setRB(processor->RB);
        rContainer->setIO(processor->IO);
        rContainer->setNEXT(processor->NEXT);
        rContainer->setSR(processor->SR);
        rContainer->setSPR(processor->SPR);
        rContainer->setPL(processor->PL);
        rContainer->setBAR(processor->BAR);
        rContainer->setPS(processor->PS);
        rContainer->setMODE(processor->MODE);
        rContainer->setPPR(processor->PPR);
        rContainer->setTIME(processor->TIME);
        rContainer->setSF(processor->SF);
    }else {
        LogManager::getSingleton().Error("RealMachine::UnloadProcessor", "processor or processorcontainer ptr is null");
    }
}
